package com.icetec.iceintel.ui.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.ArrayList;

import com.icetec.R;
import com.icetec.iceintel.database.DevicesListSqlHelper;
import com.icetec.iceintel.database.SchemeControlSqlHelper;
import com.icetec.iceintel.framework.AnimatedExpandableListView;
import com.icetec.iceintel.model.Device;
import com.icetec.iceintel.model.Scheme;
import com.icetec.iceintel.netrequest.UniResAllocator;

/**
 * Created by amo on 2015/3/14.
 * 可展开LISTVIEW的设配器
 */
public class SchemeSelectAdapter  extends AnimatedExpandableListView.AnimatedExpandableListAdapter {


    private Context mContext ;
    final Integer POSITION_BRIGHTERNESS=0;
    final Integer POSITION_COLORTEMP=1;

    private ArrayList<Device> deviceArrayList= new ArrayList<Device>();
    DevicesListSqlHelper devicesListSqlHelper;
    SchemeControlSqlHelper schemeControlSqlHelper;
    LayoutInflater layoutInflater;
    Scheme scheme;


    public SchemeSelectAdapter (Context context) {
        mContext = context;
        devicesListSqlHelper = new DevicesListSqlHelper();
        schemeControlSqlHelper=new SchemeControlSqlHelper();
        deviceArrayList.addAll(devicesListSqlHelper.getAllDeviceEceeptGroup());
        layoutInflater = LayoutInflater.from(mContext);
        scheme=new Scheme();



    }



    public ArrayList<Device> getDealWithList(){
        ArrayList<Device> vailedList=new ArrayList<Device>();
        for(Device device:deviceArrayList){
            if(device.getExtra()!=null&&device.getExtra().equals("yes")){
                vailedList.add(device);
            }
        }
        return vailedList;
    }


    public boolean areAllItemsEnabled() {
        return false;
    }

    /*
     * 设置子节点对象，在事件处理时返回的对象，可存放一些数据
     */

    public ArrayList<Integer> getChild(int groupPosition, int childPosition) {
        ArrayList<Integer> propertyList=new ArrayList<Integer>();
        propertyList.add(Integer.parseInt(deviceArrayList.get(groupPosition).getBrightness()));
        propertyList.add(Integer.parseInt(deviceArrayList.get(groupPosition).getColortemp()));
        return propertyList;

    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getRealChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final seekBarHolder seekBarHolder;
        final  ArrayList<Integer> arrayList=getChild(groupPosition,childPosition);

        // if(convertView==null){

        convertView=(View)layoutInflater.inflate(R.layout.component_lightscale_items,null);
        seekBarHolder=new seekBarHolder();
        seekBarHolder.seekBarBrightness=(SeekBar)convertView.findViewById(R.id.lightscale_seekBar);
        seekBarHolder.seekBarColortemp=(SeekBar)convertView.findViewById(R.id.tempscale_seekBar);
        //    convertView.setTag(seekBarHolder);
        //   }
        //   else
        //   {
        //  seekBarHolder=(seekBarHolder)convertView.getTag();
        //   }


        seekBarHolder.seekBarBrightness.setProgress(arrayList.get(POSITION_BRIGHTERNESS));
        seekBarHolder.seekBarColortemp.setProgress(arrayList.get(POSITION_COLORTEMP));

        seekBarHolder.seekBarBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                getGroup(groupPosition).setBrightness(String.valueOf(progress));

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        seekBarHolder.seekBarColortemp.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                getGroup(groupPosition).setColortemp(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        return convertView;
    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return 1;
    }

    /*
     * 返回分组对象，用于一些数据传递，在事件处理时可直接取得和分组相关的数据
     */
    public Device getGroup(int groupPosition) {
        return deviceArrayList.get(groupPosition);
    }

    /*
     * 分组的个数
     */
    public int getGroupCount() {
        return deviceArrayList.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /*
     * 分组视图，这里也是一个文本视图
     */
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        final LightListHolder lightListHolder;
        final  Device device=(Device)getGroup(groupPosition);

        if(convertView==null){

            convertView=(View)layoutInflater.inflate(R.layout.component_addgroup_lightitems,null);
            lightListHolder=new LightListHolder();
            lightListHolder.lightName=(TextView)convertView.findViewById(R.id.lightlist_item_group_tv);
            lightListHolder.lightIcon=(ImageView)convertView.findViewById(R.id.scheme_devicelist_icon_iv);
            lightListHolder.lightSelector=(CheckBox)convertView.findViewById(R.id.lightlist_item_group_checkbox);
            lightListHolder.itemLayout=(RelativeLayout)convertView.findViewById(R.id.lightlist_item_group_rely);
            convertView.setTag(lightListHolder);
        }
        else
        {
            lightListHolder=(LightListHolder)convertView.getTag();
        }


        if(device.getExtra()==null||device.getExtra().equals("no")){

            lightListHolder.lightSelector.setChecked(false);

        }

        else{
            lightListHolder.lightSelector.setChecked(true);

        }


        if(isExpanded){

            device.setExtra("yes");
            lightListHolder.lightSelector.setChecked(true);

        }

        else{

            lightListHolder.lightSelector.setChecked(false);
            device.setExtra("no");

        }


        if(device.getOnoff()!=null&&device.getOnoff().equals("on")){

            lightListHolder.lightIcon.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(), true));

        }

        else {

            lightListHolder.lightIcon.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(),false));
        }

        if(device.getName()!=null) {
            lightListHolder.lightName.setText(device.getName());

        }


        else {
            lightListHolder.lightName.setText(R.string.unnamed_light);
        }

        return  convertView;
    }

    /*
     * 判断分组是否为空，本示例中数据是固定的，所以不会为空，我们返回false
     * 如果数据来自数据库，网络时，可以把判断逻辑写到这个方法中，如果为空
     * 时返回true
     */
    public boolean isEmpty() {
        return false;
    }

    /*
     * 收缩列表时要处理的东西都放这儿
     */
    public void onGroupCollapsed(int groupPosition) {

    }

    /*
     * 展开列表时要处理的东西都放这儿
     */
    public void onGroupExpanded(int groupPosition) {

    }

    /*
     * Indicates whether the child and group IDs are stable across changes to
     * the underlying data.
     */
    public boolean hasStableIds() {
        return false;
    }

    /*
     * Whether the child at the specified position is selectable.
     */
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    public class LightListHolder {
        ImageView lightIcon;
        CheckBox lightSelector;
        TextView lightName;
        RelativeLayout itemLayout;
    }

    public class seekBarHolder {
       SeekBar seekBarBrightness;
       SeekBar seekBarColortemp;
    }
}
